package com.nation.controller;

import com.nation.domain.TImportantLiteratureData;
import com.nation.domain.User;
import com.nation.serviceImpl.ImportantLiteratureDataServiceImpl;
import com.nation.utils.ReadAndStoreFile;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by 凌 on 2017/11/25.
 */
@Controller
@RequestMapping(value = "/literatureData")
public class ImportantLiteratureDataController {
    @Autowired
    ImportantLiteratureDataServiceImpl LiteratureDataServiceImpl;
    @Autowired
    com.nation.service.NationInfoService NationInfoService;

    @RequestMapping(value = "uploadLiteratureData.do",method = RequestMethod.POST)
    @ResponseBody
    public String uploadLiteratureData(HttpServletRequest request, @RequestParam(value = "nation_z")String nation_z,@RequestParam(value = "document_type")int document_type,
                                       @RequestParam(value = "document")MultipartFile document,
                                       @RequestParam(value = "document_source")String document_source){

        org.apache.shiro.subject.Subject currentuser= SecurityUtils.getSubject();
        User user=(User)currentuser.getPrincipal();
        String input_code=user.getYHZH();
        TImportantLiteratureData tImportantLiteratureData=new TImportantLiteratureData();
        tImportantLiteratureData.setRecoderId(input_code);
        tImportantLiteratureData.setDocumentSource(document_source);
        tImportantLiteratureData.setDocumentType(document_type);
        try {
            String path = request.getSession().getServletContext().getResource("/").getPath();
            path += ReadAndStoreFile.ImportantLiteratureData;

            System.out.println("path--" + path);

//          将MultipartFile格式转换成File
            String file_path = ReadAndStoreFile.save(document, path);
            file_path=file_path.substring(file_path.indexOf("nation/importantLiteratureData"));
            tImportantLiteratureData.setDocument(file_path);
            String country_code = NationInfoService.getCountryCodeByNation_z(nation_z);
            tImportantLiteratureData.setCountryCode(country_code);

            LiteratureDataServiceImpl.uploadLiteratureData(tImportantLiteratureData);

        } catch (IOException e) {
            e.printStackTrace();
            return "上传失败";
        }
        return "上传成功";
    }

}
